package fem2.gui.src;

import inf.vtk.VTKConvert;
import vtk.vtkProgrammableSource;
import fem2.gui.ModelHolder;
import fem2.gui.ModelObserver;

public abstract class HofemSource extends vtkProgrammableSource implements ModelObserver {

	static {
		VTKConvert.loadLibs();
	}

	protected ModelHolder modelHolder_;

	public HofemSource() {
		/*
		 * hbui: to tell vtkProgrammableSource the execute method is method
		 * execute below
		 */
		SetExecuteMethod(this, "execute");
	}

	protected abstract void doExecute();

	public void doExecuteNoModel() {
	}

	public final void execute() {
		/*
		 * hbui: this is function vtk call to execute
		 */
		// quick return
		if (modelHolder_ == null) {
			doExecuteNoModel();
			return;
		}

		// execute
		try {
			doExecute();
		} catch (Throwable t) {
			t.printStackTrace();
		}
	}

	public ModelHolder getModel() {
		return modelHolder_;
	}

	protected abstract void modelChanged();

	public final void setModel(ModelHolder model) {
		if (modelHolder_ != null) {
			modelHolder_.removeObserver(this);
		}

		// remember new model
		modelHolder_ = model;
		modelHolder_.addObserver(this);

		// tag as modified
		modelChanged();
		solutionDataChanged();
	}

	public void solutionChanged(ModelHolder m) {
		solutionDataChanged();
	}

	protected abstract void solutionDataChanged();
}
